/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xclient.cpp
 *
 * Purpose: xclient define and implementation
 *
 * Developer:
 *   wen.gu , 2023-10-30
 *
 * TODO:
 *
 ***************************************************************************/

#include "xbus/com/xclient.h"
#include "xbus/com/xdialer.h"

namespace xbus::com {

using namespace xbus::core;

XClient::~XClient() {
    /** todo something */
}

XClient::XClient(XClientHandle handle)
    :impl_(std::move(handle)){
    /** todo something */
}


XResultCode XClient::start(const XClientStartParam& param) {
    return impl_->start(param);
}

XResultCode XClient::stop() {
    return impl_->stop();
}



XResultCode XClient::subscribe(MessageId event_id, OnMessageHandler event_handler) {
    //impl_->subscribe()
    return { };
}

XResultCode XClient::subscribe(const EventHandleArray& events) {
    return { };
}

XResultCode XClient::unsubscribe(MessageId event_id) {
    return { };
}

XResultCode XClient::unsubscribe(const EventHandleArray& events) {
    return { };
}


XResultCode XClient::requestAsync(XMessageSp msg_ptr, OnMessageHandler response_handler /*= nullptr*/ ) {
    return { };
}


//static 
XResult<XClientHandle> XClient::FindService(const std::string& url, int32_t port /*= 0*/, int32_t timeout_ms /*= core::WaitType::NoWait*/ ) {
    auto res = XDialer::FindService(url, port, timeout_ms);
    if (!res) {
        return XResult<XClientHandle>::FromError(res.error());
    }

    XClientHandle client = std::make_unique<internal::XClientImpl>(res.value());

    if (!client) {
        return XResult<XClientHandle>::FromError(XErrC::InsufficientReousces);
    }

    return client;
}

//static 
XResult<FindServiceHandle> XClient::StartFindService(const std::string& url, int32_t port, OnFindServiceHandler handler) {
    return XDialer::StartFindService(url, port, [handler](XDialerSp dialer) {
        XClientHandle client = std::make_unique<internal::XClientImpl>(dialer);

        if (client) {
            return handler(std::move(client));
        }        
    });
}

//static 
XResultCode XClient::StopFindService(const FindServiceHandle& handle) {
    return XDialer::StopFindService(handle);
}

} //namespace xbus::com